class JsonResponse(object):
    """
    统一响应格式
    """

    def __init__(self, code, msg, data, status):
        self.code = code
        self.msg = msg
        self.data = data
        self.status = status

    @classmethod
    def success(cls, data=None, status="200"):
        return cls(200, 'success', data, status)

    @classmethod
    def success_with_msg(cls, msg='success'):
        return cls(200, msg, None)

    @classmethod
    def error(cls, msg='error', status="error"):
        return cls(500, msg, None, status)

    @classmethod
    def accident_error(cls, data=None, status="error"):
        return cls(501, 'error', data, status)

    def to_dict(self):
        return {
            'code': self.code,
            'msg': self.msg,
            'data': self.data,
            'status': self.status
        }
